package com.itheima.day13.thread.test15;

// 生产者类
public class Cooker implements Runnable {
    private Desk desk;

    public Cooker(Desk desk) {
        this.desk = desk;
    }


    @Override
    public void run() {
        int count = 0;
        while (true) {
            // 上锁
            synchronized (desk.getObj()) {
                // 判断做完了没
                if (desk.getCount() == 0)
                    break;
                // 判断桌子上有无包子
                if (desk.isBoo()) {
                    // 有 ， 等
                    try {
                        desk.getObj().wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                } else {
                    // 无 , 做 ，叫
                    System.out.println("厨师正在做包子......");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("做完了,做了第" + (++count) + "个包子\n");
                    desk.setBoo(true);
                    desk.getObj().notify();
                }
            }
        }
    }
}
